#pragma once

#include "oj_model2.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    const std::string template_path = "./template-html/";
    const std::string webroot_path = "./wwwroot/";
    using namespace ns_model;
    using namespace ns_util;
    class View
    {
    public:
        View()
        {}
        ~View()
        {}

        // struct Question
        // {
        //     std::string _qid; // 题目编号
        //     std::string _title; // 题目
        //     std::string _difficulty; // 难度
        //     int _cpu_limit; // 时间限制
        //     int _mem_limit; // 空间限制
        //     std::string _desc; // 题目的描述
        //     std::string _header; // 题目中编辑器里的内容
        //     std::string _tail; // 测试用例，与_header拼接形成完整代码
        // };

        void AllExpandHtml(const std::vector<Question>& questions, std::string* html, const std::string& name)
        {
            std::string src_html = template_path + "all_questions.html";
            // 形成数据字典
            ctemplate::TemplateDictionary root("all_questions");

            for(const auto& q : questions)
            {
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question_list");
                sub->SetValue("qid", q._qid);
                sub->SetValue("title", q._title);
                sub->SetValue("difficulty", q._difficulty);
            }

            root.SetValue("name", name);
            // 获取要被渲染的 html，不删除 html 源代码中的空白字符
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            // 开始渲染
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const Question& q, std::string* html, const std::string& name)
        {
            std::string src_html = template_path + "one_question.html";
            ctemplate::TemplateDictionary root("one_question");

            root.SetValue("qid", q._qid);
            root.SetValue("title", q._title);
            root.SetValue("difficulty", q._difficulty);
            root.SetValue("desc", q._desc);
            root.SetValue("header", q._header);
            root.SetValue("name", name);

            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &root);
        }

        void GetAlreadyLoginHtml(std::string* html, const std::string& name)
        {
            std::string src_html = webroot_path + "AlreadyLogin.html";
            ctemplate::TemplateDictionary root("one_question");

            root.SetValue("name", name);

            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &root);
        }

        bool GetLoginHtml(std::string* html)
        {
            std::string login_path =  webroot_path + "login.html";
            if(!FileUtil::ReadFileBinary(login_path, html))
            {
                std::string ErrorPath = webroot_path + "error.html";
                *html = FileUtil::ReadFileBinary(ErrorPath, html);
                return false;
            }
            return true;
        }

        bool GetRegHtml(std::string* html)
        {
            std::string login_path =  webroot_path + "register.html";
            if(!FileUtil::ReadFileBinary(login_path, html))
            {
                std::string ErrorPath = webroot_path + "error.html";
                *html = FileUtil::ReadFileBinary(ErrorPath, html);
                return false;
            }
            return true;
        }

        bool GetindexHtml(std::string* html)
        {
            std::string login_path =  webroot_path + "index.html";
            if(!FileUtil::ReadFileBinary(login_path, html))
            {
                std::string ErrorPath = webroot_path + "error.html";
                *html = FileUtil::ReadFileBinary(ErrorPath, html);
                return false;
            }
            return true;
        }
    private:

    };
}